package com.tanhua.dubbo.mongo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;

import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi{

    @Autowired
    private MongoTemplate mongoTemplate;


    /*
    * 搜附近
    *   distance --半径 单位 米
    *
    * */
    public List<Long> search(Long userId, Long distance) {
        //根据userId获取UserLocation 判断一下如果没有该用户地理位置信息 返回一个空集合
        Criteria criteria = Criteria.where("userId").is(userId);
        UserLocation ul = mongoTemplate.findOne(Query.query(criteria), UserLocation.class);

        //获得地理位置信息
        GeoJsonPoint location = ul.getLocation();

        //根据地理位置为圆点,distance为半径 构造半径
        //构造函数参数 :double value -->半径长度, Metric metric --> 半径长度单位
        Distance dis=new Distance(distance/1000, Metrics.KILOMETERS);

        //根据半径绘制圆形
        //构造函数参数 Point center--地理位置, Distance radius 半径
        Circle circle=new Circle(location,dis);

        //查询
        //根据圆形构造Criteria
        Criteria criteria1 = Criteria.where("location").withinSphere(circle);
        Query query = Query.query(criteria1);
        List<UserLocation> list = mongoTemplate.find(query, UserLocation.class);

        List<Long> ids = CollUtil.getFieldValues(list, "userId", Long.class);

        return ids;
    }

    /*
     * latitude 纬度
     * longitude  经度
     * addrStr 位置
     *
     * 上报地理信息
     * */
    public void save(Long userId, Double latitude, Double longitude, String addrStr) {

        //判断该用户的地理坐标是否在表中
        Criteria criteria = Criteria.where("userId").is(userId);
        UserLocation ul = mongoTemplate.findOne(Query.query(criteria), UserLocation.class);
        if (ul==null){
            //创建对象返回
            UserLocation userLocation=new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
            mongoTemplate.save(userLocation);
        }else {
            // 存在就更新
            Query query = Query.query(criteria);
            Update update=new Update();
            update.set("location",new GeoJsonPoint(longitude,latitude));
            update.set("addrStr",addrStr);
            update.set("updated",System.currentTimeMillis());
            update.set("lastUpdated",System.currentTimeMillis());

            mongoTemplate.updateFirst(query,update,UserLocation.class);

        }





    }


}
